package com.btkjgc.utils;

import java.util.ArrayList;
import java.util.List;

import com.btkjgc.models.AdminUser;
 

public class FtlOperationUtil
{
 

	@SuppressWarnings("unchecked")
	public static Integer getEntityIdByUserName(String userName)
	{
		try
		{
			String ntityhql = "from AdminUser where username=" + "'" + userName
					+ "'";
			List<AdminUser> entitys = HibernateUtil
					.executeQuery(ntityhql, null);
			for (AdminUser e : entitys)
			{
				return e.getEntityid();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

 
	public static boolean isNull(Object obj)
	{
		return (obj == null) || ("".equals(obj));
	}

	@SuppressWarnings("rawtypes")
	public static boolean exist(String selectHql, String index)
	{
		try
		{
			if (!isNull(index))
			{
				List list = HibernateUtil.executeQuery(selectHql,
						new String[] { index });
				return list.size() > 0;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return true;
	}

	 

	@SuppressWarnings("unchecked")
	public static String getAdminNameByAdminId(Integer id)
	{
		try
		{
			String hql = "from AdminUser where id=" + "'" + id + "'";
			List<AdminUser> adminUserInfos = HibernateUtil.executeQuery(hql,
					null);
			for (AdminUser e : adminUserInfos)
			{
				return e.getUsername();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	 

	public static String getStringType(String intType)
	{
		String ret = "";
		try
		{
			if (!FtlOperationUtil.isNull(intType))
			{
				String[] intT = intType.split(",");
				int length = intT.length;
				for (int i = 0; i < length; i++)
				{
					String tString = DataDisplayUtil.getTypeName(3);
					ret += DataDisplayUtil.getType(Integer.valueOf(intT[i]), 1,
							tString);
					if (i < (length - 1))
					{
						ret += "，";
					}
				}
			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return ret;
	}

	@SuppressWarnings({ "unchecked" })
	public static String getBuyerEmailByUerName(String userName)
	{
		String ret = "";
		try
		{
			String hql = "select distinct buyer_email from PayRecord where username = "
					+ userName;
			List<String> list = HibernateUtil.executeQuery(hql, null);
			int i = 0;
			for (String s : list)
			{
				ret += s;
				if (i < (list.size() - 1))
				{
					ret += ",";
				}
				i++;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return ret;
	}

	public static String getTicketIdByTicketName(String ticketName)
	{
		try
		{
			String hql = "select id from TicketT where name = ?";
			String id = (String) HibernateUtil.objectQuery(hql,
					new String[] { ticketName });
			return id;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	public static String getTicketNameByTicketId(String ticketId)
	{
		try
		{
			String hql = "select name from TicketT where id = ?";
			String id = (String) HibernateUtil.objectQuery(hql,
					new String[] { ticketId });
			return id;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	public static List<String> getCarNumberListOfEntity(Integer entityid)
	{
		try
		{
			String hql = "select carnumber from CarBasicInfo ";
			if (entityid == Constant.ADMIN_ENTITY)
			{
				hql += " WHERE 1 = 1";
			}
			else
			{
				hql += " WHERE entityid = " + entityid;
			}
			return HibernateUtil.executeQuery(hql, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return new ArrayList<String>();
	}

	@SuppressWarnings("unchecked")
	public static List<String> getAllEntityName()
	{
		try
		{
			String entityhql = "select name from Entity ";
			List<String> entityNames = HibernateUtil.executeQuery(entityhql,
					null);
			return entityNames;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return new ArrayList<String>();
	}
}
